using System.Globalization;
using Localization.Shared;
using MinimalApi.Sample;
using Microsoft.AspNetCore.Localization;

var builder = WebApplication.CreateBuilder(args);

var supportedCultures = new[] 
{ 
	new CultureInfo("en"), 
	new CultureInfo("zh") 
};

builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
builder.Services.AddScoped<IResourceLocalizer, ResourceLocalizer>();
builder.Services.Configure<RequestLocalizationOptions>(options =>
{
  
    options.DefaultRequestCulture = new RequestCulture("en");
    options.SupportedCultures = supportedCultures;
    options.SupportedUICultures = supportedCultures;
});

var app = builder.Build();
app.UseRequestLocalization();
app.UseHttpsRedirection();
app.MapGet("/articles/{id}", (
    int id, 
    IResourceLocalizer resourceLocalizer
    ) => 
{
    var article = Data.GetArticles().FirstOrDefault(a => a.Id == id);
    if (article == null)
    {
        return Results.NotFound();
    }
    var articleDto = new ArticleDto
    {
        Name = resourceLocalizer.Localize(article.NameKey),
        Description = resourceLocalizer.Localize(article.DescriptionKey)
    };
    return Results.Ok(articleDto);
});
app.Run();
